package com.thinksns.sociax.t4.android.event;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.component.CustomTitle;
import com.thinksns.sociax.component.LeftAndRightTitle;
import com.thinksns.sociax.t4.adapter.AdapterEventUserList;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;
import com.thinksns.sociax.thinksnsbase.utils.UnitSociax;

import java.util.List;

public class EventDetailAllMemberActivity extends ThinksnsAbscractActivity implements OnRefreshListener,OnLoadmoreListener, AdapterView.OnItemClickListener {

    private SmartRefreshLayout smartRefreshLayout;
    private ListView mListView;
    protected String eid;
    protected AdapterEventUserList userAdapter;
    private int mCurrentPage = 1;

    @Override
    public String getTitleCenter() {
        return "全部成员";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntentData();
        initView();
        initListener();
        initData();
    }

    private void initData() {
        smartRefreshLayout.autoRefresh();
        userAdapter = new AdapterEventUserList(this, null);
        mListView.setAdapter(userAdapter);
    }

    protected void initListViewAttr() {
        mListView.setDivider(new ColorDrawable(0x72cccccc));
        mListView.setDividerHeight(UnitSociax.dip2px(this, 0.2f));
        mListView.setSelector(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    }

    private void initIntentData() {
        if (!getIntent().hasExtra("eid")) {
            throw new AssertionError("no eid in intent at EventDetailAllMemberActivity!");
        } else {
            eid = getIntent().getStringExtra("eid");
        }
    }

    private void initListener() {
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadmoreListener(this);
        mListView.setOnItemClickListener(this);
    }

    protected void initView() {
        smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smart_refresh_layout);
        this.mListView = (ListView) findViewById(R.id.pull_refresh_list);
        initListViewAttr();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_event_detail_all_member;
    }

    @Override
    protected CustomTitle setCustomTitle() {
        return new LeftAndRightTitle(R.drawable.img_back, this);
    }

    public void doLoadData(int page, final boolean isRefresh) {
        ((Thinksns) getApplication()).getApiEvent().getEventUsers(eid, page, new ApiHttpClient.HttpResponseListener() {
            @Override
            public void onSuccess(Object result) {
                smartRefreshLayout.finishRefresh();
                smartRefreshLayout.finishLoadmore();

                userAdapter.setHasData();
                if (isRefresh) {
                    userAdapter.resetData((List<ModelUser>) result);
                } else {
                    userAdapter.addData((List<ModelUser>) result);
                }
            }

            @Override
            public void onError(Object result) {
                smartRefreshLayout.finishRefresh();
                smartRefreshLayout.finishLoadmore();
                if (!((String) result).equals(getString(R.string.load_fail))) {//由于没有数据的回执 --;
                    if (userAdapter.getCount() == 0) {
                        userAdapter.setNoData();
                    }
                    mCurrentPage--;
                }
                ToastUtils.showToast((String) result);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (!userAdapter.getNoData()) {
            if (userAdapter.getItem(position - 1).getSpace_privacy() == ModelUser.CAN_NOT_TO_HOME) {
                ToastUtils.showToast(R.string.tip_no_oauth_to_other_home);
            } else {
                Intent intent = new Intent(this, ActivityUserInfo_2.class);
                intent.putExtra("uid", userAdapter.getItem(position - 1).getUid());
                startActivity(intent);
            }
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mCurrentPage++;
        doLoadData(mCurrentPage, false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mCurrentPage = 1;
        doLoadData(mCurrentPage, true);
    }
}
